package com.mycompany.crm.controller;

import com.mycompany.crm.annotation.LogAnno;
import com.mycompany.crm.base.BaseController;
import com.mycompany.crm.base.ResultInfo;
import com.mycompany.crm.model.TreeModel;
import com.mycompany.crm.service.ModuleService;
import com.mycompany.crm.vo.Module;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author jy
 * @version 1.0
 * @date 2022/2/24 11:15
 */
@Controller
@RequestMapping("module")
public class ModuleController extends BaseController {

    @Resource
    private ModuleService moduleService;


    /**
     * 查询所有的资源列表
     * @return
     */
    @LogAnno(description = "查询所有的资源列表")
    @RequestMapping("queryAllModules")
    @ResponseBody
    public List<TreeModel> queryAllModules(Integer roleId){
        return moduleService.queryAllModules(roleId);
    }

    /**
     * 打开授权页面
     * @param roleId
     * @return
     */
    @LogAnno(description = "打开授权页面")
    @RequestMapping("toAddGrantPage")
    public String toAddGrantPage(Integer roleId, Model model){
        model.addAttribute("roleId",roleId);
        return "role/grant";
    }

    /**
     * 进入资源管理页面
     * @return
     */
    @LogAnno(description = "进入资源管理页面")
    @RequestMapping("index")
    public String index(){
        return "module/module";
    }

    /**
     * 查询资源列表
     * @return
     */
    @LogAnno(description = "查询资源列表")
    @RequestMapping("list")
    @ResponseBody
    public Map<String,Object> queryModuleList(){
        return moduleService.queryModuleList();
    }


    /**
     * 打开添加资源的页面
     * @param grade 层级
     * @param parentId 父菜单Id
     * @return
     */
    @LogAnno(description = "打开添加资源的页面")
    @RequestMapping("toAddModulePage")
    public String toAddModulePage(Integer grade,Integer parentId,Model model){
        model.addAttribute("grade",grade);
        model.addAttribute("parentId",parentId);
        return "module/add";
    }


    /**
     * 添加资源
     * @param module
     * @return
     */
    @LogAnno(description = "添加资源")
    @PostMapping("add")
    @ResponseBody
    public ResultInfo addModule(Module module){
        moduleService.addModule(module);
        return success("资源添加成功！");
    }

    /**
     * 打开修改资源的页面
     * @param id
     * @param model
     * @return
     */
    @LogAnno(description = "打开修改资源的页面")
    @RequestMapping("toUpdateModulePage")
    public String toUpdateModulePage(Integer id,Model model){
        Module module = moduleService.selectByPrimaryKey(id);
        model.addAttribute("module",module);
        return "module/update";
    }

    /**
     * 修改资源
     * @param module
     * @return
     */
    @LogAnno(description = "修改资源")
    @PostMapping("update")
    @ResponseBody
    public ResultInfo updateModule(Module module){
        moduleService.updateModule(module);
        return success("资源修改成功！");
    }

    /**
     * 删除资源
     * @param id
     * @return
     */
    @LogAnno(description = "删除资源")
    @PostMapping("delete")
    @ResponseBody
    public ResultInfo deleteModule(Integer id){
        moduleService.deleteModule(id);
        return success("资源删除成功！");
    }
}
